#pragma once

#include <vector>
#include "Defines.h"
#include "Entity.h"

class QuadTree {
public:
	typedef std::vector<Entity*> EntityList;

	QuadTree();
	QuadTree(int x,int y, int width, int height);
	QuadTree(sf::Vector2i pos, sf::Vector2i size);
	QuadTree(const Rect& rect);
	~QuadTree();

	void Subdivide(int level);
	void Subdivide();

	void Insert(Entity* entity);
	void Delete(Entity* entity);
	void FindNearbyEntities(Entity* entity, EntityList& neighbours);
	void FindEntitiesInArea(const Rect& area, EntityList& entities);

protected:
	const Rect& GetRect() const { return m_rect; }
	bool IsLeaf() const { return m_children.empty(); }

private:
	typedef std::vector<QuadTree> QuadTreeList;
	QuadTreeList m_children;
	bool m_isLeaf;
	EntityList m_entities;
	Rect m_rect;

};